1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
import { parseRequest } from '@/lib/request';
import { json, notFound, ok, unauthorized } from '@/lib/response';
import { reportSchema } from '@/lib/schema';
import { canDeleteWebsite, canUpdateWebsite, canViewReport } from '@/permissions';
import { deleteReport, getReport, updateReport } from '@/queries/prisma';
export async function GET(request: Request, { params }: { params: Promise<{ reportId: string }> }) {
const { auth, error } = await parseRequest(request);
if (error) {
return error();
}
const { reportId } = await params;
const report = await getReport(reportId);
if (!(await canViewReport(auth, report))) {
return unauthorized();
}
return json(report);
}
export async function POST(
request: Request,
{ params }: { params: Promise<{ reportId: string }> },
) {
const { auth, body, error } = await parseRequest(request, reportSchema);
if (error) {
return error();
}
const { reportId } = await params;
const { websiteId, type, name, description, parameters } = body;
const report = await getReport(reportId);
if (!report) {
return notFound();
}
if (!(await canUpdateWebsite(auth, websiteId))) {
return unauthorized();
}
const result = await updateReport(reportId, {
websiteId,
userId: auth.user.id,
type,
name,
description,
parameters,
} as any);
return json(result);
}
export async function DELETE(
request: Request,
{ params }: { params: Promise<{ reportId: string }> },
) {
const { auth, error } = await parseRequest(request);
if (error) {
return error();
}
const { reportId } = await params;
const report = await getReport(reportId);
if (!(await canDeleteWebsite(auth, report.websiteId))) {
return unauthorized();
}
await deleteReport(reportId);
return ok();
}
|